﻿using HZU.ZP.Data.Interfaces;
using HZU.ZP.Data.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HZU.ZP.Data.Impl
{
    public class CompanyDao : ICompanyDao
    {
        private EFCoreDbContext _context;

        public CompanyDao(EFCoreDbContext context)
        {
            _context = context;
        }

        public void Add(Company company)
        {
            _context.Companies.Add(company);
            _context.SaveChanges();
        }

        public void Delete(int id)
        {
            var company = _context.Companies.FirstOrDefault(e => e.Id == id);
            company.Status = 0;
            _context.Companies.Update(company);
            _context.SaveChanges();
        }

        public Company Get(Company company)
        {
            return _context.Companies.FirstOrDefault(e => e.CompanyName == company.CompanyName && e.Email == company.Email && e.PassWord == company.PassWord && e.Status != 3);
        }

        public Company GetByEmail(string email)
        {
            return _context.Companies.FirstOrDefault(e => e.Email == email);
        }

        public Company GetByName(string companyName)
        {
            return _context.Companies.FirstOrDefault(e => e.CompanyName == companyName);
        }

        public Company GetByNumber(string companyNumber)
        {
            return _context.Companies.FirstOrDefault(e => e.CompanyNumber == companyNumber);
        }

        public IList<Company> GetComList(int status, int pageIndex)
        {
            return _context.Companies.Where(e => e.Status == status).Skip((pageIndex-1)*20).Take(20).ToList();
        }

        public Company GetId(int id)
        {
            return _context.Companies.FirstOrDefault(e => e.Id == id);
        }

        public int GetCount(int status)
        {
            return _context.Companies.Where(e => e.Status == status).Count();
        }

        public void Update(Company company)
        {
            _context.Companies.Update(company);
            _context.SaveChanges();
        }
    }
}
